package com.lottery.controller.pc28.lobbyController;

import com.alibaba.fastjson.JSON;
import com.lottery.dto.SimulationAward;
import com.lottery.obtainData.ObtainDataUtils;
import com.lottery.service.pc28.JedisClient;
import com.lottery.utils.DateUtil;
import com.lottery.utils.RandCodeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 模拟开奖数据，供测试时使用
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/test")
public class SimulationAwardController {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private JedisClient jedisClient;

	@CrossOrigin
	@RequestMapping(value = "/public/sscTestData", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
	@ResponseBody
	public String sscTestData(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Object> list = new ArrayList<Object>();
		logger.info("sscTestData={}*******************");
		try {
			SimulationAward simulationAward = new SimulationAward();
			String curFullPeoriod = ObtainDataUtils.getFullPeriod(new Date());
			Date peoriodDate = obtainTime(curFullPeoriod);
			String opentime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(peoriodDate);

			simulationAward.setExpect(curFullPeoriod);
			simulationAward.setOpentime(opentime);

			String opencode = "";
			for (int i = 0; i < 5; i++) {
				int ac = RandCodeUtil.getRandNum(0, 9);
				opencode = opencode + (ac + ",");
			}
			opencode = opencode.substring(0, opencode.length() - 1);
			simulationAward.setOpencode(opencode);
			list.add(simulationAward);
			map.put("data", list);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("*****sscTestData error******:", e);
		}
		return JSON.toJSONString(map, true);
	}

	@CrossOrigin
	@RequestMapping(value = "/public/pc28TestData", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
	@ResponseBody
	public String pc28TestData(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Object> list = new ArrayList<Object>();
		logger.info("sscTestData={}*******************");
		try {
			SimulationAward simulationAward = new SimulationAward();

			String opentime = "";
			String curTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
			int hours = Integer.parseInt(curTime.substring(11, 13));
			int minite = Integer.parseInt(curTime.substring(14, 16));
			if (hours == 23 && minite >= 55) {
				opentime = DateUtil.getDateFromString(curTime, "yyyy-MM-dd", 10) + " 09:05:00";
			} else if (hours >= 0 && hours < 9) {
				opentime = DateUtil.getFormatDate("yyyy-MM-dd") + " 09:05:00";
			} else {
				opentime = curTime.substring(0, 10);
				int delay = (minite % 5);
				minite = minite - delay;
				if (minite >= 60) {
					hours++;
					minite = minite % 60;
				}
				String time = String.format(" %02d:%02d:00", hours, minite);
				opentime = opentime + time;
			}
			simulationAward.setOpentime(opentime);

			// String tmpPeoriod = new SimpleDateFormat("yyyyMMddHH").format(new
			// Date());
			// String curFullPeoriod = tmpPeoriod +
			// opentime.substring(opentime.length() -
			// 5, opentime.length() - 3);
			// curFullPeoriod = curFullPeoriod.substring(3,
			// curFullPeoriod.length());
			String curFullPeoriod = jedisClient.incr("pc28TestExpect") + "";
			simulationAward.setExpect(curFullPeoriod);

			String opencode = "";
			for (int i = 0; i < 20; i++) {
				int ac = RandCodeUtil.getRandNum(0, 99);
				String code = String.format("%02d", ac);
				opencode = opencode + (code + ",");
			}
			opencode = opencode.substring(0, opencode.length() - 1);
			opencode = opencode + "+" + RandCodeUtil.getRandNum(0, 99);
			simulationAward.setOpencode(opencode);
			list.add(simulationAward);
			map.put("data", list);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("*****pc28TestData error******:", e);
		}
		return JSON.toJSONString(map, true);
	}

	// 开奖时间的获得
	private Date obtainTime(String expect) {
		// 开奖的时间
		String formatDate = expect.substring(0, 4) + "-" + expect.substring(4, 6) + "-" + expect.substring(6, 8);
		String formatMorning = formatDate + " 00:00:00";
		String formatForenoon = formatDate + " 10:00:00";
		String formatEvening = formatDate + " 22:00:00";
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long timeMorning = 0;
		long timeForenoon = 0;
		long timeEvening = 0;
		long timeNow = 0;
		try {
			timeMorning = simpleDateFormat.parse(formatMorning).getTime();
			timeForenoon = simpleDateFormat.parse(formatForenoon).getTime();
			timeEvening = simpleDateFormat.parse(formatEvening).getTime();
		} catch (ParseException e) {
			logger.error("*****obtainTime error******:", e);
		}
		Long NextExpectNumber = Long.valueOf(expect.substring(8, 11));
		if (NextExpectNumber >= 1 && NextExpectNumber <= 23) {
			timeNow = timeMorning + 300000 * NextExpectNumber;
		}
		if (NextExpectNumber >= 24 && NextExpectNumber <= 96) {
			timeNow = timeForenoon + 600000 * (NextExpectNumber - 24);
		}
		if (NextExpectNumber >= 97 && NextExpectNumber <= 120) {
			timeNow = timeEvening + 300000 * (NextExpectNumber - 96);
		}
		Date date = new Date(timeNow);
		logger.info("开奖的时间={}", date);
		return date;
	}
}
